package com.zz.ecommerce.togatherDeclareMgr.custDeclarationUpload;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.togatherDeclareMgr.common.bean.TContainers;
import com.zz.ecommerce.togatherDeclareMgr.common.bean.TDocuments;
import com.zz.ecommerce.togatherDeclareMgr.custDeclarationUpload.service.CargoCustService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 海关 进出口公共controller
 *
 * @author Administrator
 * @date 2015/8/15,16:30
 */
@Controller
@RequestMapping("/commonCust")
public class CargoCustController {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private CargoCustService cargoInspService;

    /**
     * 集装箱页面初始化
     *
     * @return
     */
    @RequestMapping(value = "/toContainerInfo")
    public String toContainerInfo() {
        return "/togatherDeclareMgr/custDeclarationUpload/exportCust/custContainerInfo";
    }

    /**
     * 集装箱列表查询
     *
     * @param gridRequest
     * @param container
     * @return
     */
    @RequestMapping(value = "/toContainerList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public GridResult toContainerList(GridRequest gridRequest, TContainers container) {
        if (StringUtils.isEmpty(container.getConIndx())) {
            return new GridResult(null, gridRequest, 0);
        }
        List<TContainers> list = cargoInspService.queryContainerList(gridRequest, container);
        long count = cargoInspService.getContainerCount(container);
        container.setIndx(cargoInspService.getSequence());
        return new GridResult(list, gridRequest, count);
    }


    /**
     * 集装箱信息保存
     *
     * @param container
     * @return
     */
    @RequestMapping(value = "/saveContainerInfo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public TContainers saveContainerInfo(TContainers container) {
        TContainers queryObj = new TContainers();
        queryObj.setIndx(container.getIndx());
        queryObj = cargoInspService.queryContainerInfo(queryObj);
        if (null != queryObj) {
            cargoInspService.updateContainerInfo(container);
        } else {
            cargoInspService.saveContainerInfo(container);
        }

        return container;
    }


    /**
     * 集装箱信息修改
     *
     * @param container
     * @return
     */
    @RequestMapping(value = "/updateContainerInfo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public TContainers updateContainerInfo(TContainers container) {
        cargoInspService.updateContainerInfo(container);
        container.setIndx(cargoInspService.getSequence());
        return container;
    }

    /**
     * 集装箱信息删除
     *
     * @param indx
     * @return
     */
    @RequestMapping(value = "/deleteContainerInfo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public boolean deleteContainerInfo(@RequestParam("indx") String indx) {
        TContainers container = new TContainers();
        container.setIndx(indx);
        return cargoInspService.deleteContainerInfo(container);
    }

    /**
     * 出口业务页面初始化
     *
     * @return
     */
    @RequestMapping(value = "/toCustAccessory", method = {RequestMethod.GET, RequestMethod.POST})
    public String toCustAccessory() {
        return "/togatherDeclareMgr/custDeclarationUpload/exportCust/custAccessoryFile";
    }

    /**
     * 检验检疫随附单据查询
     *
     * @param gridRequest
     * @param tDocuments
     * @return
     */
    @RequestMapping(value = "/toAccessoryFileList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public GridResult<TDocuments> toAccessoryFileList(GridRequest gridRequest, TDocuments tDocuments) {
        if (StringUtils.isEmpty(tDocuments.getConIndx())) {
            return new GridResult(null, gridRequest, 0);
        }
        List<TDocuments> list = cargoInspService.queryAccessoryList(gridRequest, tDocuments);
        long count = cargoInspService.getAccessoryCount(tDocuments);
        return new GridResult<TDocuments>(list, gridRequest, count);
    }

    /**
     * 保存随附单据
     *
     * @param documents
     * @return
     */
    @RequestMapping(value = "/saveTDocuments")
    @ResponseBody
    public String saveTDocuments(TDocuments documents) {
        if (StringUtils.isEmpty(documents.getIndx())) {
            documents.setIndx(cargoInspService.getSequence());
        }cargoInspService
        .saveDocuments(documents);
        return "success";
    }

    /**
     * 删除随附单据
     *
     * @param indx
     * @return
     */
    @RequestMapping(value = "/deleteTDocuments")
    @ResponseBody
    public boolean deleteTDocuments(String indx) {
        return cargoInspService.deleteTDocuments(indx);
    }

}
